package com.example.onlinemarket.bean.mobile;

import com.example.onlinemarket.bean.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static com.example.onlinemarket.util.FileUtil.byte2image;
import static com.example.onlinemarket.util.FileUtil.prefixPath;

public class MobileUser implements Serializable {
    private Integer userId;//用户ID
    private String userName;//用户名
    private String password;//密码
    private String email;//邮箱
    private boolean role;//角色（商家/买家）
    private String address;//收货地址
    private String imgPath;//图片路径
    private String payPassword;//支付密码

    //处理传递的MobileUser对象的敏感信息
    public static List<MobileUser> getShops(List<User> users) {
        List<MobileUser> userList = new ArrayList<>();
        for (User user: users){
            MobileUser mobileUser = new MobileUser(user);
            mobileUser.setPassword(null);
            userList.add(mobileUser);
        }
        return userList;
    }

    public String getPayPassword() {
        return payPassword;
    }

    public void setPayPassword(String payPassword) {
        this.payPassword = payPassword;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isRole() {
        return role;
    }

    public void setRole(boolean role) {
        this.role = role;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getImgPath() {
        return imgPath;
    }

    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }

    public MobileUser(User user) {
        this.userId = user.getUserId();
        this.userName = user.getUserName();
        this.password = user.getPassword();
        this.email = user.getEmail();
        this.role = user.isRole();
        this.address = user.getAddress();
        String fileName  = user.getUserId()+".jpg";
        String path = prefixPath+"user\\"+fileName;
        byte2image(user.getPhoto(), path);
        this.imgPath="/user/"+fileName;
        this.payPassword = user.getPayPassword();
    }

    @Override
    public String toString() {
        return "MobileUser{" +
                "userId=" + userId +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                ", role=" + role +
                ", address='" + address + '\'' +
                ", imgPath='" + imgPath + '\'' +
                '}';
    }
}
